package nuc.soft.xsf.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import nuc.soft.xsf.dao.ITitleTypeProjectDao;
import nuc.soft.xsf.model.Paper;
import nuc.soft.xsf.model.PaperProject;
import nuc.soft.xsf.model.ProjectTest;
import nuc.soft.xsf.model.Role;
import nuc.soft.xsf.model.Title;
import nuc.soft.xsf.model.TitlePaper;
import nuc.soft.xsf.model.Titlelog;
import nuc.soft.xsf.model.Titletype;
import nuc.soft.xsf.model.TitletypeProject;
import nuc.soft.xsf.model.User;
import nuc.soft.xsf.service.IPaperProjectService;
import nuc.soft.xsf.service.IPaperService;
import nuc.soft.xsf.service.IProjectTestService;
import nuc.soft.xsf.service.ITitlePaperService;
import nuc.soft.xsf.service.ITitleService;
import nuc.soft.xsf.service.ITitlelogService;
import nuc.soft.xsf.service.ITitletypeService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/PaperController")
public class paperController {
	@Resource
	private IProjectTestService projectService;

	@Resource
	private IPaperService paperService;

	@Resource
	private ITitleService titleService;

	@Resource
	private ITitlePaperService titlePaperService;

	@Resource
	private IPaperProjectService paperProjectService;

	@Resource
	private ITitlelogService titleLogService;

	@Resource
	private ITitletypeService  titleTypeService;
	@RequestMapping(value = "/add.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addPaper(Paper paper, ProjectTest project) {
		JSONObject json = new JSONObject();
		Random ramdon = new Random();
		int choose = 0;
		// 1.选择该父类项目所有的子评测项目
		ProjectTest project_new = new ProjectTest();
		project_new.setProjectParentId(project.getProjectId());
		Map map = Tools.transBean2Map(project_new);
		// 保存所有的题目集合
		List<TitlePaper> titlePaperList = new ArrayList();
		List<ProjectTest> list = projectService.findByCondition(map);
		// 保存选取的题目编号
		int[] list2 = new int[3];
		list2[0] = paper.getNumber();
		list2[1] = paper.getMultipltChoose();
		list2[2] = paper.getJudgementChoose();
		List titleNoList = new ArrayList();
		int flag01=0;
		for (int i = 0; i < list.size(); i++) {
			int index1 = 1;
			System.out.println(list.get(i).getProjectName());
			// 随机选择该试卷的题目,先找出子项目的类型
						String str = list.get(i).getExtend2();
						System.out.println(str);
						String[] arr = null;
						if (str.length() == 1) {
							arr = new String[1];
							arr[0] = str;
						} else {
							arr = str.split(",");
						}
			// 获取用户选取的项目名
			Paper paper01 = new Paper();
			paper01.setNumber(paper.getNumber()*arr.length);
			paper01.setTime(paper.getTime());
			paper01.setPaperName(list.get(i).getProjectName() + "-"
					+ paper.getPaperName());
			paper01.setDeletestate(0);
			paper01.setMultipltChoose(paper.getMultipltChoose()*arr.length);
			paper01.setJudgementChoose(paper.getJudgementChoose()*arr.length);
			// 新增试卷，返回试卷新增后的ID
			int paperId = paperService.insert(paper01);
			PaperProject paperProject = new PaperProject();
			paperProject.setDeletestate(0);
			paperProject.setPaperId(paperId);
			paperProject.setProjectId(project.getProjectId());
			paperProjectService.insert(paperProject);
			json.put("data", paper);
			// 通过类型找出对应的题目
			for (int j = 0; j < arr.length; j++) {
				for (int n = 0; n < list2.length; n++) {
					Title title = new Title();
					title.setTitleTypeid(Integer.parseInt(arr[j]));
					title.setExtend1(paper.getDegree().toString());
					title.setExtend2((n+1) + "");
					
					int num=list2[n];
					Map map01 = Tools.transBean2Map(title);
					// 得到该类型的所有题目
					List<Title> listTitle = titleService.findByCondition(map01);
					System.out.println(num);
					System.out.println("listTitle的大小："+listTitle.size());
					if (listTitle.size() >= num) {
						// 保存题库中所有的题目
						int[] str1 = new int[listTitle.size()];
						int max =listTitle.size() ;
						int min = 0;
						int[] flag = new int[max + 1];
						
						int index = 1;
						int count = 0;
						int flag1=0;
						while (index <= num) {
							choose = ramdon.nextInt(max);
						
							int choose01=listTitle.get(choose).getTitleId();
							// 存在该题目实体
							if (titleService.findById(choose01) != null) {
								if (flag[choose] == 0) {
									Titlelog titleLog = new Titlelog();
									titleLog.setTitleId(choose01);
									Map map02 = Tools.transBean2Map(titleLog);
									List<Titlelog> list01 = titleLogService
											.findByCondition(map02);
									if (list01.size() == 1) {
										count = Integer.parseInt(list01.get(0)
												.getExtend1());
										count++;
										System.out.print("count" + count);
										titleLog.setExtend1(count + "");
										titleLog.setTitlelogId(list01.get(0)
												.getTitlelogId());
										titleLogService.update(titleLog);
									} else {
										Titlelog titleLog01 = new Titlelog();
										titleLog01.setTitleId(choose01);
										count = 0;
										count++;
										titleLog01.setExtend1(count + "");
										titleLogService.insert(titleLog01);
									}
									titleNoList.add(choose);
									// 保存在题目试卷表中
									TitlePaper titlePaper = new TitlePaper();
									titlePaper.setPaperId(paperId);
									titlePaper.setTitleId(choose01);
									titlePaper.setExtend1(index1 + "");
									titlePaper.setDeleteState(0);
									titlePaperService.insert(titlePaper);
									titlePaperList.add(titlePaper);
									index1++;
									index += 1;
									System.out.print(choose01);
								}
								flag[choose] = 1;
							}
						}
					} else {
						json.put("lack_title", 200);
					}
				}
			}
		}
		json.put("list", titlePaperList);
		for (int z = 0; z < titleNoList.size(); z++) {
			System.out.println(titleNoList.get(z));
		}
		return json.toString();
	}

	@RequestMapping(value = "/findByCondition.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findByCondition(Paper paper) {
		JSONObject json = new JSONObject();
		Map map = Tools.transBean2Map(paper);
		List<Paper> paperList = paperService.findByCondition(map);
		json.put("paperList", paperList);
		return json.getString("paperList");
	}

	@RequestMapping(value = "/update.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String update(Paper paper) {
		paperService.update(paper);
		JSONObject json = new JSONObject();
		json.put("data", paper);
		json.put("redata", paper.getPaperId());
		return json.toString();
	}
    
	
	public List findByProjectId(Paper paper){
		JSONObject json=new JSONObject();
		PaperProject paperProject=paperProjectService.getByProjectId(paper.getPaperId());
		ProjectTest project=new ProjectTest();
		project.setProjectParentId(paperProject.getProjectId());
		Map map=Tools.transBean2Map(project);
		List<ProjectTest> list=projectService.findByCondition(map);
		List list01=new ArrayList();
		for(int i=0;i<list.size();i++){
			list01.add(list.get(i).getExtend2());
		}
		
		return list01;
		
	}
	
	@RequestMapping(value = "/findByType.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String getTitleType(Paper paper){
		List list=findByProjectId(paper);
		List list01=new ArrayList();
		for(int i=0;i<list.size();i++){
			String str=list.get(i).toString();
			String[] str1=str.split(",");
			String[] str2=new String[str1.length];
			for(int j=0;j<str1.length;j++){
				Titletype  titleType=titleTypeService.findById(Integer.parseInt(str1[j]));
				str2[j]=titleType.getTitleTypename();
			}
			list01.add(str2);
		}
		JSONObject json=new JSONObject();
		json.put("list01", list01);
		return json.getString("list01");
		
	}
	
	
	
	
	@RequestMapping(value = "/delete.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String delete(String ids) {
		String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			Paper paper = paperService.findById(Integer.parseInt(strTmp));
			PaperProject paperProject=new PaperProject();
			paperProject.setPaperId(paper.getPaperId());
			Map map=Tools.transBean2Map(paperProject);
			List<PaperProject> list02=paperProjectService.findByCondition(map);
			if(list02.size()>0){
				for(int k=0;k<list02.size();k++){
					list02.get(k).setDeletestate(1);
					paperProjectService.update(list02.get(k));
				}
			}
			if (paper != null) {
				paper.setDeletestate(1);
				paperService.update(paper);
				success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}
}
